package com.zjpavt.client.page;

import com.zjpavt.client.enums.ExtendDeviceEnum;
import com.zjpavt.client.enums.ThreePhase;
import com.zjpavt.client.extenddevice.PowerRemoteReader;
import com.zjpavt.client.hal.ExtendDeviceManager;
import com.zjpavt.client.hal.LedScreen;
import com.zjpavt.client.page.abstractpage.AbstractMultiPage;
import com.zjpavt.client.page.abstractpage.AbstractSonPage;
import com.zjpavt.client.page.intefacepage.MultiPage;
import com.zjpavt.client.page.intefacepage.ParentPage;
import com.zjpavt.client.page.pageunit.ScreenLine;
import com.zjpavt.client.util.Consts;
import com.zjpavt.client.util.NumberUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 电能表显示模块
 *
 * @author zyc
 */
public class PowerPage extends AbstractSonPage  implements MultiPage {
    /**
     * 远程抄表模块数据
     *
     * @param pageNum
     */
    public PowerPage(int pageNum, ParentPage parentPage, long mRefreshPeriod) {
        super(pageNum, parentPage, mRefreshPeriod);
    }

    /**
     * @return
     */
    @Override
    public void refreshPage() {
        PowerRemoteReader powerReader =
            (PowerRemoteReader) ExtendDeviceManager.getInstance().getExtendDevice(ExtendDeviceEnum.POWER);
        if (powerReader == null) {
            LedScreen.getInstance().setText(getContent(null));
        } else {
            LedScreen.getInstance().setText(getContent(powerReader));
        }
    }

    /**
     * * power:1526   15
     * * A_123.5_23.5
     * * B_123.5_23.5
     * * C_123.5_23.5
     *
     * @param powerRemoteReader
     * @return
     */
    private List<ScreenLine> getContent(PowerRemoteReader powerRemoteReader) {
        List<ScreenLine> screenLineList = new ArrayList<>();
        if (powerRemoteReader == null) {
            screenLineList.add(new ScreenLine.ScreenLineBuilder().text(ExtendDeviceEnum.POWER.getDeviceName()).pageNum(mPageNum).build());
            screenLineList.add(new ScreenLine.ScreenLineBuilder().text(Consts.NO_DEVICE).build());
            screenLineList.add(ScreenLine.BLANK);
            screenLineList.add(ScreenLine.BLANK);
            return screenLineList;
        }
        double kwh = powerRemoteReader.updateInformation().getKwh();
        if(getMultiPageNum()==0) {
            String powerString = "CPW:" + PowerRemoteReader.POWER_FORMAT.format(kwh);
            /*if ((int) powerRemoteReader.getCoefficient() != 0) {
                powerString = "CPOWER:" + kwh + "*" + NumberUtil.getFormat0((int) powerRemoteReader.getCoefficient());
            }*/
            screenLineList.add(new ScreenLine.ScreenLineBuilder().text(powerString).pageNum(mPageNum).build());
            screenLineList.add(new ScreenLine.ScreenLineBuilder().text(powerRemoteReader.getPhaseVoltageAndCurrent(ThreePhase.A)).build());
            screenLineList.add(new ScreenLine.ScreenLineBuilder().text(powerRemoteReader.getPhaseVoltageAndCurrent(ThreePhase.B)).build());
            screenLineList.add(new ScreenLine.ScreenLineBuilder().text(powerRemoteReader.getPhaseVoltageAndCurrent(ThreePhase.C)).build());
            return screenLineList;
        } else {
            screenLineList.add(new ScreenLine.ScreenLineBuilder().text("RPW:"+powerRemoteReader.getPower()).pageNum(mPageNum).build());
            screenLineList.add(new ScreenLine.ScreenLineBuilder().text("PGF:"+powerRemoteReader.getGridFrequency()).build());
            screenLineList.add(new ScreenLine.ScreenLineBuilder().text("COEF:"+powerRemoteReader.getCoefficient()).build());
            screenLineList.add(new ScreenLine.ScreenLineBuilder().text("").build());
            return screenLineList;
        }
    }

    @Override
    public void close() {

    }

    @Override
    public int getMultiPageNum() {
        return multiPageNum;
    }

    @Override
    public int getPageSize() {
        return 2;
    }

    @Override
    public void change2Page(int pageNum) {
        if (pageNum > getPageNum()) {
            return;
        }
        this.multiPageNum = pageNum;
        refreshPage();
    }

    @Override
    public void change2NextPage() {
        if (multiPageNum == getPageSize() - 1) {
            multiPageNum = 0;
        } else {
            multiPageNum++;
        }
        refreshPage();
    }

    @Override
    public void onKeyDown() {
        change2NextPage();
        super.onKeyDown();
    }

    @Override
    public void onKeyUp() {
        change2NextPage();
        super.onKeyUp();
    }
}
